import {defineStore} from "pinia";
import {IActions, IState} from "@/store/payModule/types";
import zjRequest from "@/services";
import {Toast} from "vant";

const usePayModule = defineStore<any, IState, any, IActions>("payModule", {
    state: () => ({
        currentChooseAddress: {
            id: 0,
            receiverProvince: 0,
            receiverCity: 0,
            receiverDistrict: 0,
            receiverAddress: "",
            receiverName: "",
            receiverMobile: "",
            receiverPhone: "",
            receiverDefault: 0
        },
        // 商品详情规格和数量相关信息
        detailSubmitInfo: {},
        // 点击订单提交的详细信息
        currentSubmitOrderInfo: {},
        // 后端获取的订单相关信息
        currentOrderDetail: {
            orderNo: "",
            price: 0
        }
    }),
    actions: {
        resetChooseAddressAction() {
            this.currentChooseAddress = {
                id: 0,
                receiverProvince: 0,
                receiverCity: 0,
                receiverDistrict: 0,
                receiverAddress: "",
                receiverName: "",
                receiverMobile: "",
                receiverPhone: "",
                receiverDefault: 0
            }
        },
        // 提交订单页面设置收货地址
        setChooseAddressAction(params) {
            this.currentChooseAddress = params
            return !!this.currentChooseAddress.id;
        },
        // 设置详情跳到订单的部分信息
        setGoodsDetailSubmitAction(params) {
            const {canPurchase, canPurchaseCount} = params
            this.detailSubmitInfo = {
                canPurchase,
                canPurchaseCount
            }
        },
        // 设置订单信息
        setOrderInfoAction({goodsNum, goodsInfo}) {
            this.currentSubmitOrderInfo = {
                goodsNum,
                goodsInfo
            }
        },
        // 设置订单信息，发请求
        async orderSubmitAction(params) {
            const {goodsNum, goodsInfo, paymentType} = params
            this.setOrderInfoAction({goodsNum, goodsInfo})
            const res = await zjRequest.post({
                url: "/order",
                data: {
                    // payId,
                    productSpecsId: this.detailSubmitInfo.canPurchase.id,
                    paymentType,
                    quantity: goodsNum,
                    productId: goodsInfo.id,
                    addressId: this.currentChooseAddress.id
                },
                needToken: true,
                needToast: true,
                needNavigate: "/payment/" + goodsInfo.id
            })
            if (res.code === 0) {
                this.currentOrderDetail = res.data
            }
            return res.code === 0
        },

        async createPayAction(params) {
            let goodsId = 0
            let needNavigate = ""
            let isSuccess = null // 判断是否成功
            let errorMessage = null // 保存失败的信息
            if (params.from === "payment") { // 付款页面过来的
                goodsId = this.currentSubmitOrderInfo.goodsInfo?.id || 0
                needNavigate = "/payment/" + goodsId
            } else if (params.from === "shopCar") {// 购物车付款过来的
                needNavigate = "/shop-car"
            } else if (params.from === "orderList") {// 订单列表的
                needNavigate = "/order"
            }
            const res = await zjRequest.post({
                url: "/pay/create",
                data: {
                    isHtml: 1,
                    payId: params.orderNo,
                    type: params.paymentType
                },
                needToken: true,
                needToast: true,
                needNavigate
            })
            isSuccess = res.data.indexOf("<script>")
            if (isSuccess !== -1) {
                return res.data
            }
            errorMessage = JSON.parse(res.data)
            Toast.fail(errorMessage.msg)
        }
    }
})
export default usePayModule
